<?php

/*
* Plugin Name: Gforms Regex Validator
* Plugin URI: https://notabug.org/wp/geforms-regex-validator
* Description: Add Regex Validator to Gravity Forms
* Author: Saeb Molaee
* Version: 1.2
* Author URI: https://saeb.sbs/
* Text Domain: geforms-regex-validator
* Domain Path: /languages
*/

if (!class_exists('GFAPI')) {
	die();
}
class GFormsRegexValidate
{
	private static $instance;
	const domain  = 'geforms-regex-validator';
	public static function get_instance()
	{
		if (self::$instance == null) {
			self::$instance = new GFormsRegexValidate();
		}
		return self::$instance;
	}
	public static function __($text, $domain = self::domain)
	{
		return __($text, $domain);
	}
	static function _load_plugin_textdomain()
	{
		load_plugin_textdomain(self::domain, false, dirname(plugin_basename(__FILE__)) . '/languages');
	}
	function add_regex_setting($position, $form_id)
	{
		//create settings on position 50 (right after Admin Label)
		$args = func_get_args();
		if ($position == 50) {
?>
			<li>
				<label for="field_regex_value" style="display:inline;">
					<?php _e("Regex", $this::domain); ?>
					<?php gform_tooltip("form_field_regex_value") ?>
				</label>
			</li>
			<li class="regex_setting field_setting">
				<input type="text" id="field_regex_value" style="direction:ltr !important" />
				<script>
					document.querySelector("#field_regex_value").addEventListener('input', function(event) {
						SetFieldProperty("regexField", event.target.value)
					})
				</script>

			</li>
		<?php
		}
	}
	function editor_script()
	{
		?>
		<script type='text/javascript'>
			//adding setting to fields of type "text"
			fieldSettings.text += ", .regex_setting";
			//binding to the load field settings event to initialize the checkbox
			try {
				jQuery(document).on("gform_load_field_settings", function(event, field, form) {
					document.querySelector("#field_regex_value").value = rgar(field, 'regexField');
				});
			} catch (e) {
				debugger;
			}
		</script>
<?php
	}
	function add_regex_tooltips($tooltips)
	{
		$tooltips['form_field_regex_value'] = "<strong>" 
		. self::__('Regex', self::domain) . "</strong>" 
		. self::__('Regex For Validation example: <p style="direction:ltr !important"> ^09\d{9}$ </p>', self::domain);
		return $tooltips;
	}
function regex_validate($result, $value, $form, $field)
	{
		$regex = rgar($field, 'regexField');
		if (strlen($regex) < 3) {
			return $result;
		}
		if (gettype($value) == 'array' && $result['is_valid']) {
			foreach($value as  $v){
				if($result['is_valid']){
					$result = $this->regex_validate($result, $v, $form, $field);
				}
			}
		}
		else {
		$validate = $value ==null || $value == "" || mb_ereg($regex, strval( $value));
		if ($result['is_valid'] && !$validate) {
			$result['is_valid'] = false;
			$result['message'] = $this::__('Format is not right');
		}
	}
		return $result;
	}
	private function __construct()
	{
		add_action('init',array( $this,'_load_plugin_textdomain'));
		add_action('gform_field_advanced_settings', array($this, 'add_regex_setting'), 10, 2);

		//Action to inject supporting script to the form editor page
		add_action('gform_editor_js', array($this, 'editor_script'));

		//Filter to add a new tooltip
		add_filter('gform_tooltips', array($this, 'add_regex_tooltips'));

		add_filter('gform_field_validation', array($this, 'regex_validate'), 10, 4);
	}
}
GFormsRegexValidate::get_instance();
